//
//  ScrollViewTableViewCell.m
//  MyProject
//
//  Created by apple on 15/11/4.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "ScrollViewTableViewCell.h"
#import "NotesModel.h"
#import "UIImageView+AFNetworking.h"

@interface ScrollViewTableViewCell ()
{
    BOOL TOUCH;
    float _rate;
    UIView *view;
}



@end

@implementation ScrollViewTableViewCell

- (void)awakeFromNib {
    
    

}


-(void)createScrollViewWithArr:(NSArray *)arr
{
    self.notesArr = arr;
    float length = 0;
    for (int i = 0;i<arr.count;i++) {
        NotesModel * model = [_notesArr objectAtIndex:i];
        
        float width = [model.width floatValue];
        float heigh = [model.height floatValue];
        float rate = width/heigh;
        float hhh =  100*rate;
        
        _rate = heigh/width;
        //往上面添加图片
        UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(length , 0, hhh, 100)];
        [self.imageScrollView addSubview:imgV];
        
        [imgV setImageWithURL:[NSURL URLWithString:model.photo_url]];
        //设置圆角
        imgV.layer.cornerRadius = 5;
        imgV.layer.masksToBounds = YES;
        
        //添加手势
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        [imgV addGestureRecognizer:tap];
        
        imgV.userInteractionEnabled = YES;
        
        length += hhh + 5;
    }
    self.imageScrollView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100);
    _imageScrollView.contentSize = CGSizeMake(length, 0);
    
    self.imageScrollView.showsHorizontalScrollIndicator = NO;
    
    
    
}

//图片被点击
-(void)tap:(UITapGestureRecognizer *)sender
{
    //隐藏navigationBar
    NSLog(@"%@",self.delegate);
    if (self.delegate && [self.delegate respondsToSelector:@selector(HidenNavigationBar)]) {
        [self.delegate HidenNavigationBar];
    }
    
    
    
    UIImageView *img = (UIImageView *)sender.view;
    //创建一个背景view和imageV
    view = sender.view.superview.superview.superview.superview.superview.superview;
    //NSLog(@"%@",_backView);
    
    if (!_backView) {
        _backView = [[UIView alloc]initWithFrame:view.bounds];
        _backView.backgroundColor = [UIColor blackColor];
    }

    float width = [UIScreen mainScreen].bounds.size.width- 20;
    float height = [UIScreen mainScreen].bounds.size.height;

    
    if (!_btn) {
        _btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_btn setTitle:@"保存到本地相册" forState:UIControlStateNormal];
        _btn.frame = CGRectMake(40, height - 100 , width - 60, 40);
        _btn.backgroundColor = [UIColor whiteColor];
        //
        _btn.layer.cornerRadius = 5;
        _btn.layer.masksToBounds = YES;
        
        //保存到本地
        [_btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];

    }
    
    
    if (!_ne) {
        _ne = [[UIImageView alloc]init];
        
        _ne.userInteractionEnabled = YES;

    }
    _ne.image = img.image;
    //给新的imageView添加收拾
    
    //添加手势
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(newTap:)];
    [_ne addGestureRecognizer:tap];
    
    _ne.center = _backView.center;
    
    __weak typeof(self)weakSelf = self;
    __weak typeof(view)weakView = view;
    
        [UIView animateWithDuration:0.3 animations:^{
        weakSelf.ne.frame = CGRectMake(10, (height - width*_rate)/2, width, width*_rate);
        [weakSelf.backView addSubview:weakSelf.ne];
        [weakView addSubview:weakSelf.backView];
    }];
    
     [_backView addSubview:_btn];
    
}

-(void)newTap:(UITapGestureRecognizer *)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showNavigationBar)]) {
        [self.delegate showNavigationBar];
    }
    
    _ne.frame = CGRectZero;
    
    [_ne removeFromSuperview];
    [_backView removeFromSuperview];
   
}

-(void)btnClicked
{
    
    [NSThread detachNewThreadSelector:@selector(downloadImage) toTarget:self withObject:nil];
}

-(void)downloadImage
{
    UIImageWriteToSavedPhotosAlbum(_ne.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
  contextInfo:(void *)contextInfo
{
    // Was there an error?
    if (error != NULL)
    {
        // Show error message...
        
    }
    else  // No errors
    {
        UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"保存成功！" message:@"已保存到相册" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [aler show];
    }
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
